<template>
  <div class="app-container">
    <div class="filter-container">
      <add-page @getLists="getLists"/>
      <batch-shift/>
    </div>
    <div>
      <el-table
        v-loading="listLoading"
        :data="list"
        :header-cell-style="{
        background: '#f2f2f2', color: '#606266', height: '10px', padding: '5px'}"
        :row-style="{
        height: '40px', padding: '0px' }"
        :cell-style="{ padding: '0px' }"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
        height="640px" 
width="80%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"/>
        <el-table-column align="center" type="index">
          <template slot-scope="scope">
            {{ (current_page - 1) * per_page + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.shift_name')" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.roster')" align="center">
          <template slot-scope="scope">
            <roster-page :row_id="scope.row.schedule_id"/>
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.operation')" align="center">
          <template slot-scope="scope">
            <edit-page :row_id="Number(scope.row.schedule_id)" @getLists="getLists"/>
            <el-button type="danger" size="mini" @click="showDelDia(scope.row.id)">{{ $t('table.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          :current-page="current_page"
          :page-sizes="[15, 50, 100]"
          :page-size="per_page"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </div>

    <!-- 删除对话框  star-->
    <el-dialog :title="$t('table.warning')" :visible.sync="delDialogVisible" width="30%">
      <span>{{ $t('table.delete_msg') }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialogVisible=false">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="del()">{{ $t('table.confirm') }}</el-button>
      </span>
    </el-dialog>
    <!-- 删除对话框 end-->
  </div>
</template>
<script>
import axios from 'axios'
import store from '@/store'
import addPage from './addPage'
import editPage from './editPage'
import rosterPage from './rosterPage'
import batchShift from './batchShift'
import {
  get_schedule_list,
  delete_schedule
} from '@/api/attendance_management/attendanceShift'

export default {
  components: {
    addPage,
    editPage,
    rosterPage,
    batchShift
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: null,
      current_page: null,
      last_page: null,
      per_page: null,
      multipleSelection: [], // 多选列表
      listQuery: {
        name: undefined,
        company: undefined,
        department: undefined,
        status: undefined,
        type: undefined,
        page: 1,
        pagesize: 10
      },
      del_id: null,
      delDialogVisible: false
    }
  },
  created() {
    this.getListspos()
  },
  methods: {
    // 获取展示列表数据
    getLists() {
      this.listLoading = false

      get_schedule_list(store.getters.token, this.listQuery).then((res) => {
        this.list = res.data.list.data
        this.current_page = res.data.list.current_page
        this.per_page = res.data.list.per_page
        this.last_page = res.data.list.last_page
        this.total = res.data.list.total
        console.log(res, '111111111111')

        this.listLoading = false
      })
    },
    getListspos() {
      this.listLoading = true
      get_schedule_list(store.getters.token, this.listQuery).then((res) => {
        this.list = res.data.list.data
        this.current_page = res.data.list.current_page
        this.per_page = res.data.list.per_page
        this.last_page = res.data.list.last_page
        this.total = res.data.list.total
        console.log(res, '111111111111')

        this.listLoading = false
      })
    },
    // 分页相关方法
    handleFilter() {
      this.listQuery.page = 1
      this.getLists()
    },
    handleSizeChange(val) {
      this.listQuery.pagesize = val
      this.getLists()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getLists()
    },
    handleSelectionChange(val) {
      this.multipleSelection = []
      val.forEach((item) => {
        this.multipleSelection.push(item.device_id)
      })
    },
    // 展示删除提示框
    showDelDia(id) {
      this.delDialogVisible = true
      this.del_id = id
    },
    // 删除
    del() {
      delete_schedule(store.getters.token, { id: this.del_id }).then((res) => {
        if (res.data.status === true) {
          this.$message.success(this.$t('table.delete_complete'))
        } else {
          this.$message.error(this.$t('table.delete_fail'))
        }
        this.getLists()
      })
      this.delDialogVisible = false
    }
  }
}
</script>
<style scoped>
.block {
  display: inline-block;
  margin-left: 20px;
}

.app-container {
  border: #eee solid 10px;
}
</style>
